//
//  KFLIsqrcodeRequest.m
//  KWNLaywerClient
//
//  Created by 孔六五 on 14/12/12.
//  Copyright (c) 2014年 张明磊. All rights reserved.
//

#import "KFLIsqrcodeRequest.h"

@implementation KFLIsqrcodeRequest
@synthesize stu;
@synthesize code;

- (void)startToRequestWithBlock:(requestResultBlock)resultBlock;
{
    self.kflEngine.hostName = @"isqrcode";
    NSMutableArray *array_sort = [NSMutableArray arrayWithObjects:stu,code,Model,Version,nil];
    NSString *string_sign = [self getSignWithArray:array_sort];
    NSString *string_url = [KWNBaseQrCodeURL stringByAppendingString:[NSString stringWithFormat:@"isqrcode?stu=%@&code=%@&src=%@&edition=%@&sig=%@",stu,code,Model,Version,string_sign]];
    
    MKNetworkOperation* op = [self.kflEngine operationWithURLString:string_url];
    [op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSError *error;
            NSDictionary *kindDic = [NSJSONSerialization JSONObjectWithData:completedOperation.responseData options:NSJSONReadingMutableLeaves error:&error];
            NSNumber *number_code = [kindDic objectForKey:@"code"];
            NSString *string_message = [kindDic objectForKey:@"msg"];
            NSMutableArray *array_result = [NSMutableArray array];
            if(!number_code || !string_message){
                [array_result addObject:@"系统发生错误"];
            }
            else{
                [array_result addObject:string_message];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                resultBlock(number_code.integerValue,array_result);
            });
        });
    }errorHandler:^(MKNetworkOperation *completedOperation,NSError *error) {
        NSMutableArray *array_data = [NSMutableArray arrayWithObject:@"获取失败"];
        resultBlock(10001,array_data);
    }];
    [self.kflEngine enqueueOperation:op];
}


@end
